home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / tkern091.zip / SRC / LINKS.C < prev    next >
Text File  |  1994-03-11  |  5KB  |  285 lines

  1. /*
  2.  *  This file forms part of "TKERN" - "Troy's Kernel for Windows".
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This file is explicitly placed in the public domain. You may use it
  7.  *  it for any purpose you see fit, including, but not limited to,
  8.  *  incorperating it into a private, commercial, public domain,
  9.  *  shareware, freeware or free software work.
  10.  */
  11.  
  12.  
  13. #include <windows.h>
  14. #include <stdarg.h>
  15. #include <dos.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <sys/tkexec.h>
  19. #include <sys/wait.h>
  20.  
  21. extern    int    tkern_valid_file(int);
  22. extern    int    tkern_errno(void);
  23. extern    void    _init_all_files(void);
  24. extern    void    _close_all_files(void);
  25. extern    void    tkern_register_program(    int    *argc,
  26.                     char    ***argv,
  27.                     char    ***envp);
  28. extern    int    tkern_close(int);
  29. extern    int    tkern_open(char const *, int, ...);
  30. extern    int    tkern_dup(int);
  31. extern    int    tkern_dup2(int, int);
  32. extern    int    tkern_read(int, char *, int);
  33. extern    int    tkern_write(int, char const *, int);
  34. extern    long    tkern_seek(int, long, int);
  35. extern    int    tkern_isatty(int);
  36. extern    int    tkern_ioctl(int, long, void *);
  37. /*
  38.  * The char * const * below should really be 
  39.  * char const * const *, but BC doesn't seem
  40.  * to like it.
  41.  */
  42. extern    int    tkern_exec(    char const *,
  43.                 void *,
  44.                 char * const *,
  45.                 char const *);
  46. extern short    tkern_wait(    union wait *wstatus);
  47. extern short    tkern_wait3(    union wait *wstatus,
  48.                 int    nFlags,
  49.                 struct rusage *pZero);
  50. extern short    tkern_waitpid(    int    pid,
  51.                 union wait *wstatus,
  52.                 int    nFlags);
  53. extern int    tkern_kill(    int    pid,
  54.                 int    nSignal);
  55.  
  56. extern    int    main(int, char **, char **);
  57.  
  58. CATCHBUF    fork_buf;
  59. extern    int    errno;
  60.  
  61. int    open(char const *pchPath, int nMode, int nAccess)
  62. {
  63.     int    rval;
  64.  
  65.     rval = tkern_open(pchPath, nMode, nAccess);
  66.     if (rval == -1)
  67.         errno = tkern_errno();
  68.     return rval;
  69. }
  70.  
  71. int    close(int fd)
  72. {
  73.     int    rval;
  74.  
  75.     rval = tkern_close(fd);
  76.     if (rval == -1)
  77.         errno = tkern_errno();
  78.     return rval;
  79. }
  80.  
  81. int    read(int fd, char *pchBuffer, int nBytes)
  82. {
  83.     int    rval;
  84.  
  85.     rval = tkern_read(fd, pchBuffer, nBytes);
  86.     if (rval == -1)
  87.         errno = tkern_errno();
  88.     return rval;
  89. }
  90.  
  91. int    write(int fd, char *pchBuffer, int nBytes)
  92. {
  93.     int    rval;
  94.  
  95.     rval = tkern_write(fd, pchBuffer, nBytes);
  96.     if (rval == -1)
  97.         errno = tkern_errno();
  98.     return rval;
  99. }
  100.  
  101. int    dup(int fd)
  102. {
  103.     int    rval;
  104.  
  105.     rval = tkern_dup(fd);
  106.     if (rval == -1)
  107.         errno = tkern_errno();
  108.     return rval;
  109. }
  110.  
  111. int    dup2(int fd1, int fd2)
  112. {
  113.     int    rval;
  114.  
  115.     rval = tkern_dup2(fd1, fd2);
  116.     if (rval == -1)
  117.         errno = tkern_errno();
  118.     return rval;
  119. }
  120.  
  121. long    lseek(int fd, long nPosition, int nStart)
  122. {
  123.     long    rval;
  124.  
  125.     rval = tkern_seek(fd, nPosition, nStart);
  126.     if (rval == -1)
  127.         errno = tkern_errno();
  128.     return rval;
  129. }
  130.  
  131. int    isatty(int fd)
  132. {
  133.     int    rval;
  134.  
  135.     rval = tkern_isatty(fd);
  136.     if (rval == -1)
  137.         errno = tkern_errno();
  138.     return rval;
  139. }
  140.  
  141. int
  142. ioctl(int fd, long nIOCtl, void *pvBuffer)
  143. {
  144.     int    rval;
  145.  
  146.     rval = tkern_ioctl(fd, nIOCtl, pvBuffer);
  147.     if (rval == -1)
  148.         errno = tkern_errno();
  149.     return rval;
  150. }
  151.  
  152. static    void
  153. doexec(    char    const *pchPath,
  154.     void    const *pvArgs,
  155.     char    * const *ppchEnv,
  156.     char    const *pchCmdLine)
  157. {
  158.     int    iTask;
  159.  
  160.     iTask = tkern_exec(pchPath, pvArgs, ppchEnv, pchCmdLine);
  161.     if (iTask == -1)
  162.         errno = tkern_errno();
  163.     Throw(fork_buf, iTask);
  164. }
  165.  
  166. void
  167. execl(    char const *pchPath, ...)
  168. {
  169.     va_list    args;
  170.  
  171.     va_start(args, pchPath);
  172.     doexec(pchPath, args, environ, 0);
  173. }
  174.  
  175. void
  176. execl_ext(    char const *pchCmdLine,
  177.         char const *pchPath,
  178.         ...)
  179. {
  180.     va_list    args;
  181.  
  182.     va_start(args, pchPath);
  183.     doexec(pchPath, args, environ, pchCmdLine);
  184. }
  185.  
  186. void
  187. execv(    char    const    *pchPath,
  188.     void    const    *pvArgs)
  189. {
  190.  
  191.     doexec(pchPath, pvArgs, environ, 0);
  192. }
  193.  
  194. void
  195. execv_ext(    char    const    *pchCmdLine,
  196.         char    const    *pchPath,
  197.         void    const    *pvArgs)
  198. {
  199.     doexec(pchPath, pvArgs, environ, pchCmdLine);
  200. }
  201.  
  202. short
  203. wait(union wait *wstatus)
  204. {
  205.     short    rval;
  206.  
  207.     rval = tkern_wait(wstatus);
  208.     if (rval == -1)
  209.         errno = tkern_errno();
  210.     return rval;
  211. }
  212.  
  213. short
  214. wait3(    union wait    *wstatus,
  215.     int        nFlags,
  216.     struct rusage    *pZero)
  217. {
  218.     short    rval;
  219.  
  220.     rval = tkern_wait3(wstatus, nFlags, pZero);
  221.     if (rval == -1)
  222.         errno = tkern_errno();
  223.     return rval;
  224. }
  225.  
  226. short
  227. waitpid(int    pid,
  228.     long    *wstatus,
  229.     int    nFlags)
  230. {
  231.     short    rval;
  232.  
  233.     rval = tkern_waitpid(pid, (union wait *) wstatus, nFlags);
  234.     if (rval == -1)
  235.         errno = tkern_errno();
  236.     return rval;
  237. }
  238.  
  239. int
  240. kill(    int    pid,
  241.     int    nSignal)
  242. {
  243.     int    rval;
  244.  
  245.     rval = tkern_kill(pid, nSignal);
  246.     if (rval == -1)
  247.         errno = tkern_errno();
  248.     return rval;
  249. }
  250.  
  251. #pragma argsused
  252. int    far    pascal    WinMain(HINSTANCE hInstance,
  253.                     HINSTANCE hPrec,
  254.                     LPSTR    lpCmdLine,
  255.                     int    nShow)
  256. {
  257.     int    argc;
  258.     char    **argv;
  259.     char    **envp;
  260.     int    rval;
  261.  
  262.     tkern_register_program(&argc, &argv, &envp);
  263.     if (!tkern_valid_file(0) ||
  264.         !tkern_valid_file(1) ||
  265.         !tkern_valid_file(2))
  266.     {
  267.         if (tkern_valid_file(0))
  268.             tkern_close(0);
  269.         if (tkern_valid_file(1))
  270.             tkern_close(1);
  271.         if (tkern_valid_file(2))
  272.             tkern_close(2);
  273.         tkern_open("/dev/window/console", O_RDWR);
  274.         tkern_dup(0);
  275.         tkern_dup(0);
  276.     }
  277.     _init_all_files();
  278.     fdopen(0, "r");
  279.     fdopen(1, "w");
  280.     fdopen(2, "w");
  281.     rval = main(argc, argv, envp);
  282.     _close_all_files();
  283.     return rval;
  284. }
  285.